package com.mdinfo.base.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.HashSet;
import java.util.List;

/**
 * Created by Administrator on 2017/11/28.
 */
public class ListUtil {

    /**
     * 求2个集合的差集(1中有,而2中没有)
     */
    public static <T> List<T> difference(List<T> list1, List<T> list2){

        HashSet<T> set1 = Sets.newHashSet(list1);
        HashSet<T> set2 = Sets.newHashSet(list2);
        Sets.SetView<T> difference = Sets.difference(set1, set2);
        return Lists.newArrayList(difference);
    }

    /**
     * 求2个集合的并集(交集)
     */
    public static <T> List<T> union(List<T> list1,List<T> list2){
        HashSet<T> set1 = Sets.newHashSet(list1);
        HashSet<T> set2 = Sets.newHashSet(list2);
        Sets.SetView<T> union = Sets.union(set1, set2);
        return Lists.newArrayList(union);
    }

    /**
     * 求2个集合的合集
     */
    public static <T> List<T> intersection(List<T> list1,List<T> list2){
        HashSet<T> set1 = Sets.newHashSet(list1);
        HashSet<T> set2 = Sets.newHashSet(list2);
        Sets.SetView<T> intersection = Sets.intersection(set1, set2);
        return Lists.newArrayList(intersection);
    }
}
